package com.demo.controller;

import com.demo.pojo.Comployee;
import com.demo.pojo.Dept;
import com.demo.pojo.User;
import com.demo.service.ComployeeService;
import com.demo.service.DeptService;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/14 09:50
 * 描述：
 */
@Controller
@RequestMapping("/comployee")
public class ComployeeController {
    @Autowired
    private ComployeeService comployeeService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("/page.do")
    @ResponseBody
    public Map<String, Object> page(
            @RequestParam(value = "comployeeName", required = false, defaultValue = "") String comployeeName,
            @RequestParam(value = "dept", required = false) String dept,
            @RequestParam(value = "sexNan", required = false) String sexNan,
            @RequestParam(value = "sexNv", required = false) String sexNv,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "0") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "");
        Comployee comployee = new Comployee();
        comployee.setComployeeName("%" + comployeeName + "%");
        comployee.setDept("".equals(dept) ? null : dept);
        if (sexNan != null && sexNv == null) {
            comployee.setSex("男");
        } else if (sexNv != null && sexNan == null) {
            comployee.setSex("女");
        }
        Page<Comployee> page = comployeeService.page(comployee, pageNo, pageSize);
        map.put("count", page.getPageTotalCount());
        map.put("data", page.getItems());
        return map;
    }


    @RequestMapping("/deptList.do")
    @ResponseBody
    public List<Dept> deptList() {
        return deptService.list();
    }

    @RequestMapping("/select.do")
    @ResponseBody
    public Map<String, Object> select(String comployeeNo) {
        Map<String, Object> map = new HashMap<>();
        Comployee comployee = null;
        try {
            comployee = comployeeService.select(comployeeNo);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "服务器发生异常");
            return map;
        }
        if (comployee != null) {
            map.put("code", 0);
            map.put("message", "查询成功！");
            map.put("data", comployee);
            return map;
        }
        map.put("code", 1);
        map.put("message", "该用户不存在，可能已被删除，请刷新页面！");
        return map;
    }

    @PostMapping("/add.do")
    @ResponseBody
    public Map<String, Object> add(Comployee comployee) {
        Map<String, Object> map = new HashMap<>();
        int add;
        try {
            add = comployeeService.add(comployee);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "添加员工时服务器发生异常！");
            return map;
        }
        if (add > 0) {
            map.put("code", 0);
            map.put("message", "添加员工成功");
            return map;
        }
        map.put("code", 1);
        map.put("message", "添加员工失败");
        return map;
    }

    @PostMapping("/update.do")
    @ResponseBody
    public Map<String, Object> update(Comployee comployee) {
        Map<String, Object> map = new HashMap<>();
        int update;
        try {
            update = comployeeService.update(comployee);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "修改员工时服务器发生异常！");
            return map;
        }
        if (update > 0) {
            map.put("code", 0);
            map.put("message", "修改员工成功");
            return map;
        }
        map.put("code", 1);
        map.put("message", "修改员工失败");
        return map;
    }

    @PostMapping("/state.do")
    @ResponseBody
    public Map<String, Object> state(
            @RequestParam(value = "no") String comployeeNo,
            @RequestParam(value = "state") String comployeeStatu) {
        Map<String, Object> map = new HashMap<>();
        Comployee comployee = new Comployee();
        comployee.setComployeeNo(comployeeNo);
        comployee.setComployeeStatu(comployeeStatu);
        int update;
        try {
            update = comployeeService.update(comployee);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "调整员工状态时服务器发生异常！");
            return map;
        }
        if (update > 0) {
            map.put("code", 0);
            map.put("message", "调整为：" + comployeeStatu);
            return map;
        }
        map.put("code", 1);
        map.put("message", "修改员工状态失败！");
        return map;
    }

    @PostMapping("/delete.do")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam(value = "no") String comployeeNo) {
        Map<String, Object> map = new HashMap<>();
        int del;
        try {
            del = comployeeService.del(comployeeNo);
        } catch (Exception e) {
            map.put("code", -1);
            map.put("message", "删除员工时服务器发生异常！");
            return map;
        }
        if (del > 0) {
            map.put("code", 0);
            map.put("message", "删除员工成功");
            return map;
        }
        map.put("code", 1);
        map.put("message", "删除员工失败");
        return map;
    }
}
